package view;

import bean.GoodInfo;
import bean.Product;
import dao.GoodInfoDao;
import dao.ProductDao;
import impl.GoodInfoDaoImpl;
import impl.ProductDaoImpl;
import util.InputUtil;
import util.ServerResponseResult;

/**
 * @classname:Product
 * @description:
 * @author: Mr.Z
 * @date: 2021/1/1 10:31
 * @version: 0.1
 * @since:1.8
 */
public class ProductView {
    private static final ProductDao productDao;

    static {
        productDao = new ProductDaoImpl();
    }

    private static int result;

    private ProductView() {
    }

    public static ServerResponseResult addProduct() {
        System.out.println("----------------");
        ProductView.showAll();
        System.out.println("欢迎来到添加商品类型页面");
        System.out.println("----------------------");
        Product product = new Product();
        System.out.println("请输入商品父Id,0为一级目录,1为二级目录");
        product.setParentId(InputUtil.inputInt());
        if (product.getParentId() == 0) {
            product.setIs_parent(1);
        } else if (product.getParentId() == 1) {
            product.setIs_parent(0);
        }
        System.out.println("请输入商品类型名称");
        product.setTypeName(InputUtil.inputNextStr());

        System.out.println("请输入类型状态,0为可用,1为不可用，2代表删除");
        product.setStatus(InputUtil.inputInt());
        result = productDao.addProduct(product);
        if (result == 1) {
            return ServerResponseResult.success();
        }
        return ServerResponseResult.error();
    }

    public static ServerResponseResult deleteProductRelyOrNo() {
        System.out.println("请选择真正删除商品类型信息/设置商品为不可用(1/2)");
        int result = InputUtil.inputInt();
        if (result == 1) {
            ProductView.showAll();
            System.out.println("请输输入要删除的商品类型id，父类型不可删除");
            int pid = InputUtil.inputInt();
            GoodInfoDao goodInfoDao = new GoodInfoDaoImpl();
            GoodInfo goodInfo = goodInfoDao.selectGoodByPid(pid);
            if(goodInfo!=null){

                return ServerResponseResult.error();
            }
            ProductDao productDao = new ProductDaoImpl();
            int i = productDao.deleteProductById(pid);
            if(i==1){
                System.out.println("商品表有关联段请查证后再删除");
                return ServerResponseResult.success();
            }
            return ServerResponseResult.error();
        }else if (result == 2) {
            ProductView.showAll();
            System.out.println("请选择要设置不可用的商品");
            int id = InputUtil.inputInt();
            ProductDao productDao = new ProductDaoImpl();
            Product product = productDao.selectProductById(id);
            product.setStatus(1);
            GoodInfoDao goodInfoDao = new GoodInfoDaoImpl();
            //查询是否有关联字段
            GoodInfo goodInfo = goodInfoDao.selectGoodByPid(id);
            if (goodInfo!=null) {
                System.out.println("商品表有关联段请查证后再删除");
                return ServerResponseResult.error();
            } else {
                int i = productDao.updateProductById(product,id);
                if (i == 1) {
                    System.out.println("设置不可用成功");
                    return ServerResponseResult.success() ;
                }
            }
        }
        return ServerResponseResult.success();
    }


    public static void updateProdut() {
        System.out.println("请选择要修改的商品类型id");
        productDao.selectAll().forEach(System.out::println);
        int id = InputUtil.inputInt();

        Product product = productDao.selectProductById(id);
        System.out.println(product);
        System.out.println("请输入要修改的字段 \n" +
                "1.父Id(0一级1二级) 2.类型名 3.状态(0.可用1.不可用.2删除)");
        String str = InputUtil.inputNextStr();
        String[] split = str.split(",");
        for (String s : split) {
            int nums = Integer.parseInt(s);

            switch (nums) {
                case 1:
                    System.out.println("请输入修改的父id");
                    product.setParentId(InputUtil.inputInt());
                    if (product.getParentId() == 0) {
                        product.setIs_parent(1);
                    } else{
                        product.setIs_parent(0);
                    }
                    break;
                case 2:
                    System.out.println("请输入要修改的类型名称");
                    product.setTypeName(InputUtil.inputNextStr());
                    break;
                case 3:
                    System.out.println("请输入要修改的产品类型状态");
                    product.setStatus(InputUtil.inputInt());
                    break;
            }
        }
        int i = productDao.updateProductById(product, id);
        if (i == 1) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
    }

    public static void showAll() {
        productDao.selectAll().forEach((k) -> {
//            if(k.getIs_parent()==1){
//                productDao.selectAll(k.getPid())
//            }else{
//
//            }

            if (k.getStatus() == 0) {
                if (k.getIs_parent() != 1) {
                    System.out.println("-----" + k);
                } else {
                    System.out.println(k);
                }
            }
        });
    }


    public static void showAllProductHaveEvery(){
        productDao.selectAll().forEach(System.out::println);
    }
}
